package com.javasoft.controller.WJC;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javasoft.entity.WJC.paperRuleEntity;
import com.javasoft.service.WJC._interface.PaperRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("paperRule")
public class PaperRuleController {

    @Autowired
    private PaperRuleService paperRuleService;

    @PostMapping("/add")
    public String add(@RequestBody paperRuleEntity paperRule) {
        Integer i = paperRuleService.addPaperRule(paperRule);
        if (i == 1) return "success";
        else return "fail";
    }

    @GetMapping("/list")
    public Page<paperRuleEntity> list(
            @RequestParam(value = "paperRuleId",required = false)    Long paperRuleId,
            @RequestParam(value = "project",required = false)    String project,
            @RequestParam(defaultValue = "1")  Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize
    )
    {
        return  paperRuleService.findPaperRules(paperRuleId, project,currentPage,pageSize);
    }

@DeleteMapping("/delete/{id}")
public ResponseEntity<?> deletePaperRule(@PathVariable long id) {
    boolean isDeleted = paperRuleService.deletePaperRule(id);
    if (isDeleted) {
        return ResponseEntity.ok("试卷规则记录已成功删除");
    } else {
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body("未找到指定的试卷规则记录");
    }
}

@PutMapping("/update/{id}")
public ResponseEntity<String> updatePaperRule(@PathVariable Long id, @RequestBody paperRuleEntity updatedPaperRuleEntity) {
    updatedPaperRuleEntity.setPaperRuleId(id);
    Integer result = paperRuleService.updatePaperRule(updatedPaperRuleEntity);
    if(result > 0) {
        return ResponseEntity.ok("试卷规则更新成功");
    } else {
        return ResponseEntity.status(500).body("试卷规则更新失败");
    }
}
}
